package com.seguriapp.common.gwt.client.celltable;

import com.google.gwt.cell.client.AbstractCell;
import com.google.gwt.cell.client.ValueUpdater;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.EventTarget;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.safehtml.client.SafeHtmlTemplates;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;

/**
 * Celda customizada que muestra un link (<a>).
 */
public class HyperlinkCell extends AbstractCell<String> {

  private String tooltip = "";
  private String style = "";

  /**
   * The HTML templates used to render the cell.
   */
  interface Templates extends SafeHtmlTemplates {

    /**
     * The template for this Cell, which includes styles and a value.
     * 
     * @param value
     *          the safe value. Since the value type is {@link SafeHtml}, it
     *          will not be escaped before including it in the template.
     *          Alternatively, you could make the value type String, in which
     *          case the value would be escaped.
     * @return a {@link SafeHtml} instance
     */
    @SafeHtmlTemplates.Template("<div><a class=\"cellTableHyperlinkCell {2}\" title=\"{1}\">{0}</a></div>")
    SafeHtml cell(SafeHtml value, String tooltip, String style);
  }

  /**
   * Create a singleton instance of the templates used to render the cell.
   */
  private static Templates templates = GWT.create(Templates.class);

  public HyperlinkCell() {
    super("click");
  }

  @Override
  public void render(final com.google.gwt.cell.client.Cell.Context context, final String value,
      final SafeHtmlBuilder sb) {
    /*
     * Always do a null check on the value. Cell widgets can pass null to cells
     * if the underlying data contains a null, or if the data arrives out of
     * order.
     */
    if (value == null) {
      return;
    }

    // If the value comes from the user, we escape it to avoid XSS attacks.
    final SafeHtml safeValue = SafeHtmlUtils.fromString(value);

    // Use the template to create the Cell's html.
    final SafeHtml rendered = templates.cell(safeValue, tooltip, style);
    sb.append(rendered);
  }

  @Override
  public void onBrowserEvent(final Context context, final Element parent, final String value,
      final NativeEvent event, final ValueUpdater<String> valueUpdater) {
    super.onBrowserEvent(context, parent, value, event, valueUpdater);

    // Handle the click event.
    if ("click".equals(event.getType())) {
      // Ignore clicks that occur outside of the outermost element.
      final EventTarget eventTarget = event.getEventTarget();
      if (Element.is(eventTarget)) {
        valueUpdater.update(value);
        event.preventDefault();
      }
    }

  }

  public void setTooltip(final String tooltip) {
    this.tooltip = tooltip;
  }

  public void setStyle(final String style) {
    this.style = style;
  }

}
